package net.obviam.starassault.view;

import net.obviam.starassault.view.StaticTextures;

import java.util.Arrays;

import net.obviam.starassault.model.Block;
import net.obviam.starassault.model.Bob;
import net.obviam.starassault.model.Bullet;
import net.obviam.starassault.model.World;
import net.obviam.starassault.screens.GameScreen;
import net.obviam.logic.Simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;


public class WorldRenderer {

	private static final float CAMERA_WIDTH = 12.8f;
	private static final float CAMERA_HEIGHT = 7.2f;

	private World world;

	/** for debug rendering **/
	ShapeRenderer debugRenderer = new ShapeRenderer();

	private GameScreen gs;
	private SpriteBatch spriteBatch;
	private int width;
	private int height;
	private float ppuX;	// pixels per unit on the X axis
	private float ppuY;	// pixels per unit on the Y axis
	private BitmapFont font;
	private Bob b;
	
	public void setSize (int w, int h) {
		this.width = w;
		this.height = h;
		ppuX = (float)width / CAMERA_WIDTH;
		ppuY = (float)height / CAMERA_HEIGHT;
	}
	
	public float getppuX(){
		return ppuX;
	}
	
	public float getppuY(){
		return ppuY;
	}

	public WorldRenderer(GameScreen gs, boolean debug) {
		this.gs = gs;
		this.world = gs.sim.world;
		spriteBatch = new SpriteBatch();
		font = new BitmapFont();
		StaticTextures.initiate();
		b = world.getBob();
	}
	
	public void camRender(){
		gs.sim.gameCam.viewportWidth = Gdx.graphics.getWidth();
		gs.sim.gameCam.viewportHeight = Gdx.graphics.getHeight();
		gs.sim.gameCam.position.set(world.getBob().getPosition().x*100+Gdx.graphics.getWidth()/3, Gdx.graphics.getHeight()/2, 0);
		gs.sim.gameCam.update();
		spriteBatch.setProjectionMatrix(gs.sim.gameCam.combined);
		spriteBatch.begin();
		drawBob();
		for(Block block : world.getBlocks()){
			if(block.getPosition().x*100 < gs.sim.gameCam.position.x+gs.sim.gameCam.viewportWidth/2){
				if(block.getPosition().x*100 > gs.sim.gameCam.position.x-gs.sim.gameCam.viewportWidth/2-ppuX){
					block.render(spriteBatch, ppuX, ppuY);
				}
			}
		}
		for(Bullet bullet : gs.sim.getBulletList()){
			spriteBatch.draw(StaticTextures.bullet, bullet.getPosition().x*ppuX, b.getPosition().y*ppuY, ppuX*0.1f, ppuY*0.1f);
		}
		if(gs.gameOver){
			drawGameOver();
			font.draw(spriteBatch, Integer.toString(b.getFinalScore()), b.getPosition().x*ppuX+500, Gdx.graphics.getHeight()/4);
		}
		drawScore();
		drawGameInterface();
		spriteBatch.end();
	}
	
	public void drawGameInterface(){
		spriteBatch.draw(StaticTextures.jump, b.getPosition().x*ppuX-200, Gdx.graphics.getHeight()/7, StaticTextures.jump.getWidth()*1.4f, StaticTextures.jump.getHeight()*1.4f);
		spriteBatch.draw(StaticTextures.fire, b.getPosition().x*ppuX+870, Gdx.graphics.getHeight()/7, StaticTextures.jump.getWidth()*1.4f, StaticTextures.jump.getHeight()*1.4f);
	}
	
	
	public void drawScore(){
		font.draw(spriteBatch, Integer.toString(b.getScore()), b.getPosition().x* ppuX, b.getPosition().y * ppuY);
		spriteBatch.draw(StaticTextures.textrandom, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/4, StaticTextures.textrandom.getWidth(), StaticTextures.textrandom.getHeight());
		spriteBatch.draw(StaticTextures.textupdown, 10500, Gdx.graphics.getHeight()/4, StaticTextures.textupdown.getWidth(), StaticTextures.textupdown.getHeight());
	}
	
	private void drawGameOver(){
		spriteBatch.draw(StaticTextures.gameover, b.getPosition().x * ppuX - Gdx.graphics.getWidth()/6, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
	}

	private void drawBob() {
		spriteBatch.draw(StaticTextures.bobTexture, b.getPosition().x * ppuX, b.getPosition().y * ppuY, Bob.SIZE * ppuX, Bob.SIZE * ppuY);
	}

}
